/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package capaPresentacion;

import capaLogica.DetalleVentaController;
import capaLogica.ProductoController;
import capaLogica.VentaController;
import capaNegocio.DetalleVenta;
import capaNegocio.Producto;
import capaNegocio.Venta;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author shivi
 */
public class FormVenta extends javax.swing.JPanel {

    ProductoController bdproducto = new ProductoController("Productos.txt");
    VentaController bdventa = new VentaController("Ventas.txt");
    DetalleVentaController bddetalleventa = new DetalleVentaController("DetalleVentas.txt");
    DefaultTableModel Modelo, Modelo2;
    String[] Titulo = {"CODIGO", "NOMBRE", "PRECIO", "STOCK", "CATEGORIA"};
    String[] Titulo2 = {"ITEM", "PRODUCTO", "PRECIO", "CANTIDAD", "SUB TOTAL"};
    String[][] datos = {};
    String[][] datos2 = {};
    int item = 1;

    public FormVenta() {
        initComponents();
        campo_NumVenta.setText("" + bdventa.nuevoCodigo());
        Modelo = new DefaultTableModel(datos, Titulo);
        Modelo2 = new DefaultTableModel(datos2, Titulo2);
        TablaProductos.setModel(Modelo);
        TablaDetalleVenta.setModel(Modelo2);
        mostrarDatosTable();

        campo_DNICLT.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (!(Character.isDigit(c)
                        || (c == KeyEvent.VK_BACK_SPACE)
                        || (c == KeyEvent.VK_DELETE))) {
                    getToolkit().beep();
                    e.consume();
                }
            }
        });

        campo_TelefonoCLT.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (!(Character.isDigit(c)
                        || (c == KeyEvent.VK_BACK_SPACE)
                        || (c == KeyEvent.VK_DELETE))) {
                    getToolkit().beep();
                    e.consume();
                }
            }
        });

        txtMontoEntregado.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (!(Character.isDigit(c)
                        || (c == KeyEvent.VK_BACK_SPACE)
                        || (c == KeyEvent.VK_DELETE))) {
                    getToolkit().beep();
                    e.consume();
                }
            }
        });

    }

    private void mostrarDatosTable() {
        Modelo = new DefaultTableModel(datos, Titulo);
        TablaProductos.setModel(Modelo);
        for (int i = 0; i < bdproducto.numeroProductos(); i++) {
            Producto dat = bdproducto.obtenerProducto(i);
            Object[] fila = {dat.getCodigo(), dat.getNombre(), dat.getPrecio(), dat.getStock(), dat.getCategoria()};
            Modelo.addRow(fila);
        }
    }

    private void mostrarItemTable() {
        Modelo2 = new DefaultTableModel(datos2, Titulo2);
        TablaDetalleVenta.setModel(Modelo2);
        for (int i = 0; i < bddetalleventa.numeroDetalleVentas(); i++) {
            DetalleVenta dat = bddetalleventa.obtenerDetalleVenta(i);
            if (dat.getNumVenta() == Integer.parseInt(campo_NumVenta.getText())) {
                String nomProducto = bdproducto.buscarProducto(dat.getCodPro()).getNombre();
                Object[] fila = {dat.getItem(), nomProducto, dat.getPrecio(), dat.getCan(), dat.getSubTot()};
                Modelo2.addRow(fila);
            }
        }
    }

    private void actualizarStock() {
        for (int i = 0; i < bddetalleventa.numeroDetalleVentas(); i++) {
            DetalleVenta dat = bddetalleventa.obtenerDetalleVenta(i);
            if (dat.getNumVenta() == Integer.parseInt(campo_NumVenta.getText())) {
                int sto = bdproducto.buscarProducto(dat.getCodPro()).getStock();
                int can = dat.getCan();
                bdproducto.actualizarStock(dat.getCodPro(), sto - can);
            }
        }
    }

    private void calcular() {
        double total = 0.0;
        for (int i = 0; i < bddetalleventa.numeroDetalleVentas(); i++) {
            DetalleVenta dat = bddetalleventa.obtenerDetalleVenta(i);
            if (dat.getNumVenta() == Integer.parseInt(campo_NumVenta.getText())) {
                total = total + dat.getSubTot();
            }
        }
        txtTotal.setText("" + total);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        TablaProductos = new javax.swing.JTable();
        jPanel6 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        jComprobante1 = new javax.swing.JLabel();
        campo_NombreClt = new javax.swing.JTextField();
        jComprobante2 = new javax.swing.JLabel();
        campo_TelefonoCLT = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        campo_DNICLT = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        campo_DireccionCLT = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        campo_NumVenta = new javax.swing.JTextField();
        txtTotal = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        txtMontoEntregado = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        txtVuelto = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        TablaDetalleVenta = new javax.swing.JTable();
        cmbTComprobante = new javax.swing.JComboBox();
        jLabel18 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        Boton_Adicionar = new javax.swing.JButton();
        jPanel9 = new javax.swing.JPanel();
        jLabel21 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        campo_Xcodigo = new javax.swing.JTextField();
        campo_Xpreciounit = new javax.swing.JTextField();
        campo_Xstock = new javax.swing.JTextField();
        campo_Xnombre = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        campo_Xcantidad = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        campo_Xtotal = new javax.swing.JTextField();
        BotonAgregarAventa = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        TablaProductos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(TablaProductos);

        jPanel6.setBackground(new java.awt.Color(247, 254, 255));
        jPanel6.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jComprobante1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jComprobante1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jComprobante1.setText("Nombre");

        jComprobante2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jComprobante2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jComprobante2.setText("DNI");

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel10.setText("Telefono");

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel11.setText("Dirrecion");

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel12.setText("Nº de venta: ");

        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel13.setText("Fecha:");

        campo_NumVenta.setEditable(false);

        txtTotal.setEditable(false);

        jLabel15.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel15.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel15.setText("Total a pagar :");

        jLabel16.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel16.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel16.setText("Monto entregado :");

        txtMontoEntregado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMontoEntregadoActionPerformed(evt);
            }
        });

        jLabel17.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel17.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel17.setText("Vuelto :");

        txtVuelto.setEditable(false);

        TablaDetalleVenta.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(TablaDetalleVenta);

        cmbTComprobante.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Boleta", "Factura" }));
        cmbTComprobante.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbTComprobanteActionPerformed(evt);
            }
        });

        jLabel18.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel18.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel18.setText("Tipo comprobante:");

        jButton1.setText("VENDER");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextField1.setEditable(false);
        jTextField1.setText(new SimpleDateFormat("dd/MM/yyyy").format(new Date()));

        jButton2.setText("Eliminar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addGap(48, 48, 48)
                        .addComponent(campo_NumVenta, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel18)
                        .addGap(18, 18, 18)
                        .addComponent(cmbTComprobante, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 449, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jComprobante1)
                        .addGap(48, 48, 48)
                        .addComponent(campo_NombreClt, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jComprobante2, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(57, 57, 57)
                        .addComponent(campo_DNICLT, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50)
                        .addComponent(jLabel10)
                        .addGap(11, 11, 11)
                        .addComponent(campo_TelefonoCLT, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel11)
                        .addGap(43, 43, 43)
                        .addComponent(campo_DireccionCLT, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 409, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel16)
                            .addGroup(jPanel6Layout.createSequentialGroup()
                                .addComponent(jButton2)
                                .addGap(18, 18, 18)
                                .addComponent(jButton1)
                                .addGap(60, 60, 60)
                                .addComponent(jLabel15))
                            .addComponent(jLabel17))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtVuelto, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtMontoEntregado, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(campo_NumVenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel13)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jLabel12)))
                .addGap(6, 6, 6)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jLabel18))
                    .addComponent(cmbTComprobante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComprobante1)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(campo_NombreClt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(7, 7, 7)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComprobante2)
                    .addComponent(campo_DNICLT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(campo_TelefonoCLT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel11)
                    .addComponent(campo_DireccionCLT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2)))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel15)
                            .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel16)
                            .addComponent(txtMontoEntregado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(9, 9, 9)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtVuelto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel17))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        Boton_Adicionar.setText("Adicionar");
        Boton_Adicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Boton_AdicionarActionPerformed(evt);
            }
        });

        jPanel9.setBackground(new java.awt.Color(247, 254, 255));
        jPanel9.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel21.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel21.setText("Id Producto:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("Stock :");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Precio Und :");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("Cantidad :");

        campo_Xcodigo.setEditable(false);

        campo_Xpreciounit.setEditable(false);
        campo_Xpreciounit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campo_XpreciounitActionPerformed(evt);
            }
        });

        campo_Xstock.setEditable(false);

        campo_Xnombre.setEditable(false);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("Nombre :");

        campo_Xcantidad.setEditable(false);
        campo_Xcantidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campo_XcantidadKeyPressed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("Total :");

        campo_Xtotal.setEditable(false);

        BotonAgregarAventa.setText("Agregar");
        BotonAgregarAventa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonAgregarAventaActionPerformed(evt);
            }
        });

        jButton3.setText("Cancelar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(campo_Xstock, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel9Layout.createSequentialGroup()
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel21))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(campo_Xcodigo, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                            .addComponent(campo_Xnombre)))
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(53, 53, 53)
                        .addComponent(campo_Xtotal))
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                        .addComponent(campo_Xpreciounit, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel9Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(campo_Xcantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addComponent(BotonAgregarAventa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton3)))
                .addContainerGap())
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campo_Xcodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel21, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campo_Xnombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(campo_Xstock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campo_Xpreciounit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campo_Xcantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campo_Xtotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BotonAgregarAventa)
                    .addComponent(jButton3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                        .addComponent(Boton_Adicionar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(105, 105, 105))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Boton_Adicionar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void campo_XpreciounitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campo_XpreciounitActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campo_XpreciounitActionPerformed

    private void campo_XcantidadKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campo_XcantidadKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            int cant = Integer.parseInt(campo_Xcantidad.getText());
            double preU = Double.parseDouble(campo_Xpreciounit.getText());
            if (cant > 0) {
                campo_Xtotal.setBackground(Color.white);
                campo_Xtotal.setText(String.valueOf(cant * preU));
            } else {
                JOptionPane.showMessageDialog(null, "Cantidad INCORRECTA!");
                campo_Xcantidad.setText(null);
            }
        }
    }//GEN-LAST:event_campo_XcantidadKeyPressed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        //Borrar_Campos();
        campo_Xcantidad.setEditable(false);
        campo_Xcantidad.setBackground(Color.LIGHT_GRAY);
        campo_Xtotal.setBackground(Color.LIGHT_GRAY);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void Boton_AdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Boton_AdicionarActionPerformed
        int Fila_Adicionar = TablaProductos.getSelectedRow();
        if (Fila_Adicionar >= 0) {
            campo_Xcodigo.setText(String.valueOf(TablaProductos.getValueAt(Fila_Adicionar, 0)));
            campo_Xnombre.setText(String.valueOf(TablaProductos.getValueAt(Fila_Adicionar, 1)));
            campo_Xstock.setText(String.valueOf(TablaProductos.getValueAt(Fila_Adicionar, 3)));
            campo_Xpreciounit.setText(String.valueOf(TablaProductos.getValueAt(Fila_Adicionar, 2)));
            campo_Xcantidad.setEditable(true);
            campo_Xcantidad.setBackground(Color.white);
        } else {
            JOptionPane.showMessageDialog(null, "SELECCIONE UNA FILA!");
        }
    }//GEN-LAST:event_Boton_AdicionarActionPerformed

    private void BotonAgregarAventaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonAgregarAventaActionPerformed
        if (campo_Xcantidad.getText().equals("")) {
            JOptionPane.showMessageDialog(BotonAgregarAventa, "Escriba la cantidad de productos");
        } else {
            int numventa = Integer.parseInt(campo_NumVenta.getText());
            int codpro = Integer.parseInt(campo_Xcodigo.getText());
            double precio = Double.parseDouble(campo_Xpreciounit.getText());
            int cantidad = Integer.parseInt(campo_Xcantidad.getText());
            campo_Xtotal.setText("" + precio * cantidad);
            int stock = Integer.parseInt(campo_Xstock.getText());
            double subtotal = Double.parseDouble(campo_Xtotal.getText());
            int codigo = numventa * 10 + item;
            if (cantidad < stock) {
                bddetalleventa.adicionarDetalleVenta(new DetalleVenta(codigo, numventa, item, codpro, cantidad, precio, subtotal));
                bdproducto.guardar();
                limpiarAgregado();

                JOptionPane.showMessageDialog(BotonAgregarAventa, "Item agregado correctamente ");
                mostrarItemTable();
                item++;
                calcular();
            } else {
                JOptionPane.showMessageDialog(null, "Stock Insuficiente");
                campo_Xcantidad.setText("");
                campo_Xtotal.setText("");
            }

        }

    }//GEN-LAST:event_BotonAgregarAventaActionPerformed
    private void limpiarAgregado() {
        campo_Xcantidad.setText("");
        campo_Xnombre.setText("");
        campo_Xcodigo.setText("");
        campo_Xstock.setText("");
        campo_Xpreciounit.setText("");
        campo_Xtotal.setText("");
    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (campo_DNICLT.getText().equals("")) {
            JOptionPane.showMessageDialog(BotonAgregarAventa, "Rellenar DNI");
        } else {
            int numven = Integer.parseInt(campo_NumVenta.getText());
            String fecha = "02/05/2012";
            String tcomprobante = (String) cmbTComprobante.getSelectedItem();
            int cliente = Integer.parseInt(campo_DNICLT.getText());
            double tventa = Double.parseDouble(txtTotal.getText());
            bdventa.adicionarVenta(new Venta(numven, fecha, tcomprobante, cliente, tventa));
            bdventa.guardar();
            bddetalleventa.guardar();
            actualizarStock();
            bdproducto.guardar();
            JOptionPane.showMessageDialog(null, "Producto(s) vendidos");
            limpiartextos();

        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void cmbTComprobanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbTComprobanteActionPerformed

        if (cmbTComprobante.getSelectedIndex() == 1) {
            jComprobante1.setText("Razon Social");
            jComprobante2.setText("RUC");
        } else {
            jComprobante1.setText("Nombre");
            jComprobante2.setText("DNI");
        }
    }//GEN-LAST:event_cmbTComprobanteActionPerformed

    private void txtMontoEntregadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMontoEntregadoActionPerformed
        double total = Double.parseDouble(txtTotal.getText());
        double entregado = Double.parseDouble(txtMontoEntregado.getText());
        double vuelto = entregado - total;
        txtVuelto.setText("" + vuelto);
    }//GEN-LAST:event_txtMontoEntregadoActionPerformed
    private void limpiartextos() {
        campo_Xnombre.setText("");
        campo_Xcodigo.setText("");
        campo_Xstock.setText("");
        campo_Xpreciounit.setText("");
        campo_Xtotal.setText("");
        campo_DNICLT.setText("");
        campo_TelefonoCLT.setText("");
        campo_NombreClt.setText("");
        campo_DireccionCLT.setText("");
        campo_NumVenta.setText("" + bdventa.nuevoCodigo());
        txtTotal.setText("");
        cmbTComprobante.setSelectedIndex(0);
        TablaDetalleVenta.setModel(new DefaultTableModel());

    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        Modelo2.removeRow(TablaDetalleVenta.getSelectedRow());
        //  DetalleVenta udv=bddetalleventa.buscarDetalleVenta(item);
        //  bddetalleventa.eliminarDetalleVenta(udv);
    }//GEN-LAST:event_jButton2ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonAgregarAventa;
    private javax.swing.JButton Boton_Adicionar;
    private javax.swing.JTable TablaDetalleVenta;
    private javax.swing.JTable TablaProductos;
    private javax.swing.JTextField campo_DNICLT;
    private javax.swing.JTextField campo_DireccionCLT;
    private javax.swing.JTextField campo_NombreClt;
    private javax.swing.JTextField campo_NumVenta;
    private javax.swing.JTextField campo_TelefonoCLT;
    public javax.swing.JTextField campo_Xcantidad;
    public javax.swing.JTextField campo_Xcodigo;
    public javax.swing.JTextField campo_Xnombre;
    public javax.swing.JTextField campo_Xpreciounit;
    public javax.swing.JTextField campo_Xstock;
    private javax.swing.JTextField campo_Xtotal;
    private javax.swing.JComboBox cmbTComprobante;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jComprobante1;
    private javax.swing.JLabel jComprobante2;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField txtMontoEntregado;
    private javax.swing.JTextField txtTotal;
    private javax.swing.JTextField txtVuelto;
    // End of variables declaration//GEN-END:variables
}
